import axios from "axios";
import {observable} from "mobx";

class Service {
    @observable reg_status = false;
    @observable login_status = false;
    reg(obj, username, password, role){
        console.log(username, password);
        axios.post("/api/reg/", {"username": username, "password": password, "role": role})
            .then(response => {
                obj.sccessed();
                this.reg_status = true;
            })
            .catch(error => obj.failed());
    }

    login(obj, username, password, remember){
        axios.post("/api/login/", {"username": username, "password": password, "remember": remember})
            .then(response => {
                obj.sccessed();
                this.login_status = true;
            })
            .catch(error => obj.failed());
    }

    check_role(obj){
        axios.get("/api/result/check_role/")
            .then(response => obj.loading_wait = true)
            .catch(error =>
            {if (error.response.status === 400){
                obj.privilege_alarm();
            }else if(error.response.status === 401){
                obj.is_not_login();
            }
            })
    }

    input_result(obj, results){
        axios.post("/api/result/input_results/", {"results": results})
            .then(response => obj.sub_success())
            .catch(error => obj.sub_failed());
    }

    query_result(obj, stu_id){
        console.log(stu_id);
        axios.post("/api/result/query_result/", {"stu_id": stu_id})
            .then(response => {
                console.log(response.data);
                obj.setState({"data":[{"key":1, "stu_id": response.data["stu_id"], "chinese": response.data["chinese"],
                        "math": response.data["math"], "english": response.data["english"]}]});
                console.log(obj.data);
            })
            .catch(error =>{
                if (error.response.status === 400){
                    obj.input_error();
                }
                if (error.response.status === 402){
                    obj.stu_id_notexist();
                }
            })
    }

    quit_login(){
        axios.get("/api/login/quit/");
    }
}


let service = new Service();
export {service};